package net.uhuijia.shop.info.service.impl;

import net.uhuijia.enums.ErrorCodeEnum;
import net.uhuijia.enums.IsBond;
import net.uhuijia.enums.ShopVerifyState;
import net.uhuijia.exception.BusinessException;
import net.uhuijia.shop.info.service.ShopService;
import net.uhuijia.shop.usershop.service.UserShopService;
import net.uhuijia.user.useridcard.service.UserIdCardService;
import net.uhuijia.user.usermall.persistence.model.UserMall;
import net.uhuijia.vo.shop.ShopBaseDetaiVo;
import net.uhuijia.vo.shop.ShopDetailVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import net.uhuijia.appbase.service.CrudService;
import net.uhuijia.shop.info.persistence.dao.ShopDao;
import net.uhuijia.shop.info.persistence.model.Shop;
import net.uhuijia.shop.info.persistence.model.ShopExample;


@Service
@Transactional
public class ShopServiceImpl
     extends CrudService<ShopDao,Shop,ShopExample> implements ShopService{
    @Autowired
    private ShopDao shopDao;

    @Autowired
    private UserIdCardService userIdCardService;
    @Autowired
    private UserShopService userShopService;

    /**
     * @Author xzj
     * @Description //获取店铺基本信息
     * @Date  2018/10/6
     * @Param
     * @return
     **/
    @Override
    public ShopBaseDetaiVo getShopBaseDetaiVoByShopId(String shopId) {
        if(StringUtils.isEmpty(shopId))
            throw  new IllegalArgumentException();
        Shop shop = this.get(shopId);
        if(shop!=null){
            ShopBaseDetaiVo shopBaseDetaiVo = new ShopBaseDetaiVo();
            shopBaseDetaiVo.setShopImage(shop.getShopImage());
            shopBaseDetaiVo.setShopName(shop.getShopName());
            shopBaseDetaiVo.setTotalCollect(shop.getShopTotalCollect());
            shopBaseDetaiVo.setTotalGoodCount(1000);
            shopBaseDetaiVo.setTotalSaleCount(1000);
            shopBaseDetaiVo.setTotalStar(shop.getShopTotalStar());
            shopBaseDetaiVo.setAddress("xxxxxxxxxxxxxxxxxxxxxxxxx");
            return shopBaseDetaiVo;
        }else{
            throw  new BusinessException(ErrorCodeEnum.shop_not_exist_error);
        }
    }

    @Override
    public ShopDetailVo getShopDetailVoByShopId(String shopId, UserMall userMall) {
        if(StringUtils.isEmpty(shopId))
            throw  new IllegalArgumentException();
        Shop shop = this.get(shopId);
        if(shop!=null){
            ShopDetailVo shopDetailVo = new ShopDetailVo();
            /**店铺基本信息*/
            shopDetailVo.setShopImage(shop.getShopImage());
            shopDetailVo.setShopName(shop.getShopName());
            shopDetailVo.setTotalCollect(shop.getShopTotalCollect());
            shopDetailVo.setTotalGoodCount(1000);
            shopDetailVo.setTotalSaleCount(1000);
            shopDetailVo.setTotalStar(shop.getShopTotalStar());
            shopDetailVo.setAddress("xxxxxxxxxxxxxxxxxxxxxxxxx");
            //TODO 判断店铺是否被用户收藏
            shopDetailVo.setIsCollect("0");
            return shopDetailVo;
        }else{
            throw  new BusinessException(ErrorCodeEnum.shop_not_exist_error);
        }
    }
    /**
     * @Author xzj
     * @Description //初始化店铺基本信息
     * @Date  2018/10/7
     * @Param
     * @return
     **/
    public Shop generalShop(){
        Shop shop = new Shop();
        shop.setIsPayBond(IsBond.No.getBond());
        shop.setState(ShopVerifyState.Verifying.getState());
        shop.setShopTotalStar(3);
        shop.setShopTotalCollect(0);
        return shop;
    }

    @Override
    public Shop joinShop(UserMall userMall, String shopId,String realName, String phone,String idCardFrontImg, String idCardBackImg) {
        //添加用户身份证基本信息
        //一个店铺一个身份信息 目前不涉及到多个店铺
        this.userIdCardService.addUserIdCard(userMall,realName,idCardFrontImg,idCardBackImg);
        //添加店铺信息
        Shop shop = this.generalShop();
        this.add(shop);
        //用户绑定店铺信息
        this.userShopService.addUserShop(userMall,shop);
        return shop;
    }
}